from django import http
from django.shortcuts import render
from django.views import View
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ReadOnlyModelViewSet

from apps.category.models import Category
from apps.serializers.catgory import CatgoryListSerializer


class Cook_Category(APIView):
    def get(self, reuqest):
        queryset = Category.objects.all()
        data = []
        for i in queryset:
            if not i.parent_id:
                datas = {"cName": i.name, "child": []}
                for j in queryset:
                    if j.parent_id == i.id:
                        datas["child"].append({"id": j.id, "cName": j.name})
                data.append(datas)
        print(data)
        res = {"status": 200, "data": data}
        return Response(res)


# class Cook_Category(ReadOnlyModelViewSet):
#
#     # 指定序列化器类
#     serializer_class = CatgoryListSerializer
#     queryset = Category.objects.all()
#
#     def list(self, request, *args, **kwargs):
#         queryset = self.filter_queryset(self.get_queryset())
#         print(queryset)
#
#         serializer = self.get_serializer(queryset, many=True)
#
#         data = []
#         for i in serializer.data:
#             if not i["parent_id"]:
#                 datas = {"cName": i["name"], "child": []}
#                 for j in serializer.data:
#                     if j["parent_id"] == i["id"]:
#                         datas["child"].append({"id": j["id"], "cName": j["name"]})
#                 data.append(datas)
#         print(serializer.data)
#         print(type(serializer.data))
#         return Response(data)


